package com.ryskoo.catering.dinnertable.repository;

import com.ryskoo.catering.dinnertable.domain.DinnerTablePO;
import com.ryskoo.framework.core.repository.BasicJPAImpl;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class DinnerTableJPAImpl extends BasicJPAImpl<DinnerTablePO> implements IDinnerTableRepository {
    @Override
    protected String getSelectFields(Class clazz) throws NoSuchFieldException {
        return "o.uuid,o.type_uuid as typeUuid, o.no,o.qrcode, o.name, o.max_person as maxPerson, o.using_status as usingStatus, o.booking_type as bookingType, o.create_time as createTime, d.name as typeName";
//        return "o.*, d.name as typeName";
    }

    @Override
    protected String getJoinTable() throws NoSuchFieldException {
        return " left join cater_dinnertable_type d on o.type_uuid = d.uuid ";
        /*return " LEFT JOIN " + getTableName(DinnerTableTypePO.class) + " d ON o." +
                getFieldName(DinnerTablePO.class, "typeUuid") + " = d." + getFieldName(DinnerTableTypePO.class, "uuid");*/
    }

   /* @Override
    protected Query getSqlQuery(String sql, Class modelClass) {
        return getH4Session().createSQLQuery(sql).setResultTransformer(Transformers.aliasToBean(modelClass));
    }*/

    @Override
    public List<String> getUuidsByTypeUuid(String typeUuid) {
        String hql = "select o.uuid from DinnerTablePO o where o.typeUuid = :typeUuid";
        Query query = getH4Session().createQuery(hql);
        query.setString("typeUuid",typeUuid);
        return query.list();
    }

    @Override
    public List<String> getUuidByNoAndVendorId(String no, String vendorId) {
        String hql = "select o.uuid from DinnerTablePO o where o.no = :no and o.vendorUuid = :vendorUuid";
        Query query = getH4Session().createQuery(hql);
        query.setString("no",no);
        query.setString("vendorUuid",vendorId);
        return query.list();
    }

    @Override
    public List<String> getUuidByNoAndVendorIdForUpdate(String no, String vendorId, String uuid) {
        String hql = "select o.uuid from DinnerTablePO o where o.no = :no and o.vendorUuid = :vendorUuid and uuid != :uuid";
        Query query = getH4Session().createQuery(hql);
        query.setString("no",no);
        query.setString("vendorUuid",vendorId);
        query.setString("uuid",uuid);
        return query.list();
    }
    
    @Override
	public void updateState(String uuid, String state) {
    	String hql = "update DinnerTablePO set usingStatus=:usingStatus where uuid = :uuid ";
        Query query = getH4Session().createQuery(hql);
        query.setString("uuid",uuid);
        query.setString("usingStatus",state);
        query.executeUpdate();
	}
}
